<?php
class CausesController extends AppController {

	var $name = 'Causes';

	function index() {
                $this->set('title_for_layout', 'Secuestros - Causas');
                $this->loadModel('Courts');
                $this->loadModel('Charter');
                $this->loadModel('Placement'); 
                $this->loadModel('Jurisdiction'); 
                $this->Cause->recursive = 0;  
		$this->set('causes', $this->paginate());
				
				$policeStations = $this->Cause->PoliceStations->find('all',array('conditions' => array('PoliceStations.id'=>$master['PoliceStations']['id'])));
				
				$secretaries = $this->Cause->Secretaries->find('all');
				
				
                $courts = $this->Courts->find('all', array('conditions' => array('Courts.id' => $secretaries['0']['Secretaries']['courts_id'])));
                $charters = $this->Charter->find('all', array('conditions' => array('Charter.id' => $courts['0']['Courts']['charters_id'])));
                $placements = $this->Placement->find('all', array('conditions' => array('Placement.id' => $charters['0']['Charter']['placements_id'])));
                $jurisdictions = $this->Jurisdiction->find('all', array('conditions' => array('Jurisdiction.id' => $placements['0']['Placement']['jurisdictions_id'])));
               
                $this->set(compact('policeStations','secretaries','courts','charters','placements','jurisdictions'));
	}

	function view($id = null) {
             $this->set('title_for_layout', 'Secuestros - Causas');
             $this->loadModel('Court');
             $this->loadModel('Charter');
             $this->loadModel('Placement');
             $this->loadModel('Jurisdiction');
             $this->loadModel('Province');
			 $this->loadModel('PoliceStations');
		if (!$id) {
			$this->Session->setFlash(__('Invalid cause', true));
			$this->redirect(array('action' => 'index'));
		}	
			 
             $cause = $this->Cause->find('all',array('conditions' => array('Cause.id =' => $this->Cause->id)));

			 
			 $secretaries = $this->Cause->Secretaries->find('list',array('fields'=>'descripcion'));
			 
			 
			 $courts = $this->Court->find('all',array('conditions' => array('Court.id'=>$cause['0']['Secretaries']['courts_id']),'fields'=>array('id','descripcion','placements_id')));
			
			
			 $placements = $this->Placement->find('all',array('conditions' => array('Placement.id'=>$courts['0']['Court']['placements_id']),'fields'=>array('id','charters_id','descripcion')));
			
			 
			 $charters = $this->Charter->find('all',array('conditions' => array('Charter.id'=>$placements['0']['Placement']['charters_id']),'fields'=>array('id','descripcion','jurisdictions_id','provinces_id')));
			 
			 $provinces = $this->Province->find('all',array('conditions' => array('province.id'=>$charters['0']['Charter']['provinces_id']),'fields'=>'descripcion'));
			 
			 $jurisdictions = $this->Jurisdiction->find('all',array('conditions' => array('Jurisdiction.id'=>$charters['0']['Charter']['jurisdictions_id']),'fields'=>array('id','descripcion')));

			 
			 
			
              $policeStations = $this->PoliceStations->find('all',array('conditions' => array('PoliceStations.id'=>$cause['0']['Cause']['police_stations_id']),'fields'=>array('id','descripcion')));
             
             //$policeStations = $this->Cause->PoliceStations->find('all',array('fields'=>'descripcion'));

             
             $this->set(compact('policeStations', 'secretaries','courts','charters','placements','jurisdictions','provinces'));
	     
		 $this->set('cause', $this->Cause->read(null, $id));
	}

	function add() {
             $this->set('title_for_layout', 'Secuestros - Causas');
		if (!empty($this->data)) {
			$this->Cause->create();
			if ($this->Cause->save($this->data)) {
				$this->Session->setFlash(__('The cause has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cause could not be saved. Please, try again.', true));
			}
		}
		$policeStations = $this->Cause->PoliceStations->find('list',array('fields'=>'descripcion'));
		$secretaries = $this->Cause->Secretaries->find('list',array('fields'=>'descripcion'));
		$this->set(compact('policeStations', 'secretaries'));
	}

	function edit($id = null) {
            $this->set('title_for_layout', 'Secuestros - Causas');
                $this->loadModel('Court');
                $this->loadModel('Charter');
                $this->loadModel('Placement');
                $this->loadModel('Jurisdiction');
                $this->loadModel('Province');
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cause', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Cause->save($this->data)) {
				$this->Session->setFlash(__('The cause has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cause could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Cause->read(null, $id);
		}
                $cause = $this->Cause->find('all',array('conditions' => array('Cause.id =' => $this->Cause->id)));
                $courts = $this->Court->find('all',array('conditions' => array('Court.id'=>$cause['0']['Secretaries']['courts_id']),'fields'=>array('id','descripcion','placements_id')));
                $placements = $this->Placement->find('all',array('conditions' => array('Placement.id'=>$courts['0']['Court']['placements_id']),'fields'=>array('id','descripcion','charters_id')));

                $charters = $this->Charter->find('all',array('conditions' => array('Charter.id'=>$placements['0']['Placement']['charters_id']),'fields'=>array('id','descripcion','jurisdictions_id','provinces_id')));

		$jurisdictions = $this->Jurisdiction->find('list');
               
                $policeStations = $this->Cause->PoliceStations->find('list',array('fields'=>'descripcion'));
		$secretaries = $this->Cause->Secretaries->find('list',array('fields'=>'descripcion'));
                $provinces = $this->Province->find('list');
		$this->set(compact('policeStations', 'secretaries','courts','charters','placements','jurisdictions','provinces'));
	}
        
            function update_placements(){
                
              $this->loadModel('Placement');
            if (!empty($this->data['Cause']['charters_id'])){
                   $charters_id = $this->data['Cause']['charters_id'];
                   $placements = $this->Placement->find('all', array('fields'=>array('Placement.id','Placement.descripcion'),'order'=>'id ASC','conditions'=>array('charters_id'=>$charters_id)));
                    $this->log($placements);
           }else{
                   $placements = array();    
           }                
           $this->set('options', Set::combine($placements, "{n}.Placement.id","{n}.Placement.descripcion"));
           $this->render('/causes/update_select', 'ajax');
        }
        
        function update_provinces(){
           $this->loadModel('Province');
           if (!empty($this->data['Cause']['jurisdiction_id'])){
                 $jurisdiction_id = $this->data['Cause']['jurisdiction_id'];
                 $provinces = $this->Province->find('all', array('fields'=>array('Province.id','Province.descripcion'),'order'=>'id ASC','conditions'=>array('jurisdiction_id'=>$jurisdiction_id)));
           }else{
                 $provinces = $this->Provinces->find('all', array('fields'=>array('Province.descripcion'),'order'=>'id ASC'));
           }
                $this->set('options', Set::combine($provinces, "{n}.Province.id","{n}.Province.descripcion"));
                $this->render('/causes/update_select', 'ajax');
        }
        
          function update_charters(){
          $this->loadModel('Charter');
             if (!empty($this->data['Cause']['provinces_id'])){
                   $province_id = $this->data['Cause']['provinces_id'];
                               
                   $charters = $this->Charter->find('all', array('fields'=>array('Charter.id','Charter.descripcion'),'order'=>'id ASC','conditions'=>array('provinces_id'=>$province_id)));
            }else{
                  $charters = $this->Charter->find('all', array('fields'=>array('Charter.descripcion'),'order'=>'id ASC'));
             }
                $this->set('options', Set::combine($charters, "{n}.Charter.id","{n}.Charter.descripcion"));
                $this->render('/causes/update_select', 'ajax'); 
        }
        
        function update_courts(){
             $this->loadModel('Court');
             if (!empty($this->data['Cause']['charters_id'])){
                   $charters_id = $this->data['Cause']['charters_id'];
                   $courts = $this->Court->find('all', array('fields'=>array('Court.id','Court.descripcion'),'order'=>'id ASC','conditions'=>array('charters_id'=>$charters_id)));
             }else{
                  $courts = $this->Court->find('all', array('fields'=>array('Court.descripcion'),'order'=>'id ASC'));
             }
                $this->set('options', Set::combine($courts, "{n}.Court.id","{n}.Court.descripcion"));
                $this->render('/causes/update_select', 'ajax'); 
        } 
       
        function update_secretaries(){
             $this->loadModel('Secretary');
             if (!empty($this->data['Cause']['courts_id'])){
                   $courts_id = $this->data['Cause']['courts_id'];
                   $secretaries = $this->Secretary->find('all', array('fields'=>array('Secretary.id','Secretary.descripcion'),'order'=>'id ASC','conditions'=>array('courts_id'=>$courts_id)));
             }else{
                  $secretaries = $this->Secretary->find('all', array('fields'=>array('Secretary.descripcion'),'order'=>'id ASC'));
             }
                $this->set('options', Set::combine($secretaries, "{n}.Secretary.id","{n}.Secretary.descripcion"));
                $this->render('/causes/update_select', 'ajax');    
        }

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cause', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Cause->delete($id)) {
			$this->Session->setFlash(__('Cause deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Cause was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
